package com.aispeech.testcustom;

import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.aispeech.aios.sdk.AIOSForCarSDK;
import com.aispeech.aios.sdk.listener.AIOSReadyListener;
import com.aispeech.testcustom.i.IServiceForBridgeService;
import com.aispeech.testcustom.services.AiosBridgeService;
import com.aispeech.testcustom.utils.PreferenceUtil;
import com.aispeech.testcustom.utils.TiLog;

/**
 * Created by rain on 2016/11/6.
 */
public class TiApplication extends Application {
    public TiApplication() {
        super();
    }

    //public static List<Command> mlist = new ArrayList<Command>();//自定义指令集
    private static Context mContext;

    private static IServiceForBridgeService iService = null;
    private BridgeServiceConnection conn = null;

    public static Context getContext() {
        if (null == mContext) {
            TiLog.e("application context is null");
            throw new RuntimeException("unknow Error");
        }
        return mContext;
    }
    public static IServiceForBridgeService getIService(){
        if (null!=iService){
            return iService;
        }else{
            return null;
        }
    }

    public static TiApplication getApplication() {
        if (mContext == null) {
            throw new RuntimeException("Unknown Error");
        }
        return (TiApplication) mContext;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        TiLog.d("onCreate: testCustom - application");
        //是否在application初始化sdk接口,默认true
        if (PreferenceUtil.getBoolean(TiApplication.getContext(), PreferenceUtil
                .IS_INIT_SDK_IN_APPLICAION, true)) {

            TiLog.d("onCreate: application init sdk");
            AIOSForCarSDK.initialize(TiApplication.getApplication(), new AIOSReadyListener() {
                @Override
                public void onAIOSReady() {
//                    AIOSCustomizeManager.getInstance().registerCustomizeListener(new
//                            CustomizeListener(new CustomResultCallBack() {
//
//                        @Override
//                        public void setResultBack(String s) {
//                            Log.d("test", "setResultBack: application get result" + s);
//                            TiApplication.this.sendBroadcast(new Intent("aios.intent.action" +
//                                    ".application_callback_result").putExtra
//                                    ("application_result", s));
//                        }
//                    }));
//                    AIOSCustomizeManager.getInstance().regCommands(convertList4Register());

                    // 在后台启一个服务,作为语音接口回调中转
                    TiLog.d("application :onAiosReady() startService AiosBridgeService");
//                    startService(new Intent(TiApplication.this, AiosBridgeService.class));
                    conn = new BridgeServiceConnection();
                    bindService(new Intent(TiApplication.this, AiosBridgeService.class),conn,BIND_AUTO_CREATE);
                }

            });
        } else {
            TiLog.w("onCreate: application not init sdk so no bridgeService");
        }
    }


    class BridgeServiceConnection implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            iService= (IServiceForBridgeService) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }
}
